﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using WallCreateAndEdit.Properties;

namespace WallCreateAndEdit.WallModule
{
	public partial class SetWallJoinForm : System.Windows.Forms.Form
	{
		public SetWallJoinForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
		}

		public int TheType
		{
			get
			{
				return this.m_Type;
			}
		}

		public bool PickButton
		{
			get
			{
				return this.m_PickButton;
			}
			set
			{
				this.m_PickButton = value;
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void btnSingle_Click(object sender, EventArgs e)
		{
			this.m_Type = 0;
			this.ChangeImage();
			this.m_PickButton = true;
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			SetWallJoinForm.SendMessage(CmdSetWallJoin._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			SetWallJoinForm.keybd_event(bVk, 0, dwFlags, 0);
			SetWallJoinForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void btnOne_Click(object sender, EventArgs e)
		{
			this.m_Type = 1;
			this.ChangeImage();
			this.m_PickButton = true;
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			SetWallJoinForm.SendMessage(CmdSetWallJoin._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			SetWallJoinForm.keybd_event(bVk, 0, dwFlags, 0);
			SetWallJoinForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void btnMulti_Click(object sender, EventArgs e)
		{
			this.m_Type = 2;
			this.ChangeImage();
			this.m_PickButton = true;
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			SetWallJoinForm.SendMessage(CmdSetWallJoin._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			SetWallJoinForm.keybd_event(bVk, 0, dwFlags, 0);
			SetWallJoinForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetWallJoinForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				SetWallJoinForm.SendMessage(CmdSetWallJoin._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void SetWallJoinForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SetWallJoinForm.keybd_event(bVk, 0, dwFlags, 0);
			SetWallJoinForm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void btnSingle_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.toolTipSetWallJoin.GetToolTip(this.btnSingle) == this.btnSingle.Tag.ToString())
			{
				return;
			}
			this.toolTipSetWallJoin.SetToolTip(this.btnSingle, this.btnSingle.Tag.ToString());
		}

		private void btnOne_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.toolTipSetWallJoin.GetToolTip(this.btnOne) == this.btnOne.Tag.ToString())
			{
				return;
			}
			this.toolTipSetWallJoin.SetToolTip(this.btnOne, this.btnOne.Tag.ToString());
		}

		private void btnMulti_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.toolTipSetWallJoin.GetToolTip(this.btnMulti) == this.btnMulti.Tag.ToString())
			{
				return;
			}
			this.toolTipSetWallJoin.SetToolTip(this.btnMulti, this.btnMulti.Tag.ToString());
		}

		private void SetWallJoinForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void SetWallJoinForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "SetWallJoinForm.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_Type);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "SetWallJoinForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.m_Type = 0;
				this.ChangeImage();
				return true;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new Point(x, y);
			try
			{
				this.m_Type = Convert.ToInt32(streamReader.ReadLine());
				if (this.m_Type == 0)
				{
					this.btnSingle.TabIndex = 0;
				}
				else if (this.m_Type == 1)
				{
					this.btnOne.TabIndex = 0;
				}
				else
				{
					this.btnMulti.TabIndex = 0;
				}
				this.ChangeImage();
			}
			catch (Exception)
			{
				this.ChangeImage();
				streamReader.Close();
				fileStream.Close();
				return true;
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		private void ChangeImage()
		{
			if (this.m_Type == 0)
			{
				this.btnSingle.Image = Resources.Single;
				this.btnOne.Image = Resources.OneUnselected;
				this.btnMulti.Image = Resources.MultiUnselected;
				return;
			}
			if (this.m_Type == 1)
			{
				this.btnSingle.Image = Resources.SingleUnselected;
				this.btnOne.Image = Resources.One;
				this.btnMulti.Image = Resources.MultiUnselected;
				return;
			}
			this.btnSingle.Image = Resources.SingleUnselected;
			this.btnOne.Image = Resources.OneUnselected;
			this.btnMulti.Image = Resources.Multi;
		}

		private ExternalCommandData m_Revit;

		private int m_Type;

		private bool m_PickButton;
	}
}
